<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2019/1/6
 * Time: 9:59 AM
 */

namespace app\shop\command;

use think\console\command\Make;
use think\console\Input;
use think\console\Output;
use think\console\input\Option;
use think\App;

class Test extends Make
{
    protected function configure()
    {
        parent::configure();
        $this->setName('make:test')
            ->addOption('plain', null, Option::VALUE_NONE, 'Generate an empty controller class.')
            ->setDescription('Create a new resource controller class');
    }

    protected function getStub()
    {
        return APP_PATH . 'shop/command/stubs/' . lcfirst($this->type) . '.stub';
    }

    protected function buildClass($name)
    {
        $stub = file_get_contents($this->getStub());


        $namespace = trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');

        $class = str_replace($namespace . '\\', '', $name);

        $className = lcfirst($class);

        return str_replace([
            '{%ClassName%}',
            '{%namespace%}',
            '{%app_namespace%}',
            '{%className%}',
        ], [
            $class,
            $namespace,
            App::$namespace,
            $className,
        ], $stub);

    }

    protected function getNamespace($appNamespace, $module)
    {
        return parent::getNamespace($appNamespace, $module) . '\\' . lcfirst($this->type);
    }

    protected function execute(Input $input, Output $output)
    {
        $this->type = 'Controller';
        parent::execute($input, $output);

        $this->type = 'Model';
        parent::execute($input, $output);

        $this->type = 'Validate';
        parent::execute($input, $output);

        $output->writeln('TestCommand');
    }
}